﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
   public  class DBHelper
    {
       //配置文件(静态资源)
       static string str =System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
       public static SqlDataReader Zhilianshi(string sql,SqlParameter []list) {
           //创建连接对象
           SqlConnection sqlcon = new SqlConnection(str);
           //创建Command对象
           SqlCommand sqlcom = new SqlCommand(sql,sqlcon);
           if (list !=null && list.Length>0)
           {
               sqlcom.Parameters.AddRange(list);
           }
           //打开连接
           sqlcon.Open();
           //如果关联的阅读器关闭,连接也会关闭
           return sqlcom.ExecuteReader(CommandBehavior.CloseConnection);
       }
       public static DataSet Duankaishi(string sql, SqlParameter[] list)
       {
           //创建连接对象
           SqlConnection sqlcon = new SqlConnection(str);
           //创建适配器
           SqlDataAdapter da = new SqlDataAdapter(sql,sqlcon);
           //创建 虚拟内存
           DataSet ds = new DataSet();
           if (list != null && list.Length > 0)
           {
               da.SelectCommand.Parameters.AddRange(list);
           }
           //将适配器的内容赋值给虚拟内存
           da.Fill(ds);
           return  ds;
       }
       public static int Update(string sql, SqlParameter[] list)
       {
           //创建连接对象
           SqlConnection sqlcon = new SqlConnection(str);
           //创建Command对象
           SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
           if (list != null && list.Length > 0)
           {
               sqlcom.Parameters.AddRange(list);
           }
           //打开连接
           sqlcon.Open();
           int i = sqlcom.ExecuteNonQuery();
           sqlcon.Close();
           return i;
       }
       public static object Chadanzhi(string sql, SqlParameter[] list)
       {
           //创建连接对象
           SqlConnection sqlcon = new SqlConnection(str);
           //创建Command对象
           SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
           if (list != null && list.Length > 0)
           {
               sqlcom.Parameters.AddRange(list);
           }
           //打开连接
           sqlcon.Open();
           object i = sqlcom.ExecuteScalar();
           sqlcon.Close();
           return i;
       }
       //黑道查询
       public static DataTable Dataset(string sql, List<SqlParameter> l)
       {
           SqlConnection con = new SqlConnection(str);
           SqlDataAdapter dp = new SqlDataAdapter(sql, con);
           if (l != null && l.Count > 0)
           {
               dp.SelectCommand.Parameters.AddRange(l.ToArray());
           }
           DataTable da = new DataTable();
           dp.Fill(da);
           return da;
       }
    }
}
